/*
 * ValueDescription.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

/**
 * A ValueDescription is used to describe the property or field of an object
 * (for debugging tools).
 */
public class ValueDescription {

	/** The eol. */
	private static String eol = System.getProperty("line.separator") == null ? "\n"
			: System.getProperty("line.separator");

	/**
	 * The name of this value (for example the property name if it is a
	 * property), null if not known or irrelevant.
	 */
	public String name = null;

	/** The string describing the type of this value. */
	public String type;

	/** The string describing the value. */
	public String value;

	/**
	 * Build a value descriptor for an unknown name.
	 * 
	 * @param type
	 *            the type
	 * @param value
	 *            the value
	 */
	public ValueDescription(String type, String value) {
		this.type = type;
		this.value = value;
	}

	/**
	 * Build a value descriptor for a specified name.
	 * 
	 * @param name
	 *            the name
	 * @param type
	 *            the type
	 * @param value
	 *            the value
	 */
	public ValueDescription(String name, String type, String value) {
		this.name = name;
		this.type = type;
		this.value = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String propertyValue = value;
		// Remove leading eol
		while (propertyValue.indexOf("\n") == 0) {
			propertyValue = propertyValue.substring(1);
		}
		while (propertyValue.indexOf(eol) == 0) {
			propertyValue = propertyValue.substring(eol.length());
		}
		// limit size
		if (propertyValue.length() > 250) {
			propertyValue = propertyValue.substring(0, 250) + "...";
		}
		// keep only first line
		int ieol = propertyValue.indexOf(eol);
		if (ieol == -1)
			ieol = propertyValue.indexOf("\n");
		if (ieol != -1) {
			propertyValue = propertyValue.substring(0, ieol) + "...";
		}

		if (name == null) {
			return type + ": " + propertyValue;
		} else {
			return name + ": [" + type + "]: " + propertyValue;
		}
	}
}
